package com.hxd.otc.module.wallet.activity;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.wallet.adapter.TransferRecordAdapter;
import com.hxd.otc.module.wallet.bean.TransferRecoderBean;
import com.hxd.otc.module.wallet.contract.TansferRecoderContact;
import com.hxd.otc.module.wallet.presenter.TransferRecoderPresenter;
import com.hxd.otc.widget.RecyclerListDivider;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;

/**
 * 包  名 :  com.hxd.otc.module.wallet.activity
 * 类  名 :  TransferRecordActivity
 * 作  者 :  zjl
 * 时  间 :  2019/4/25 15:24
 * 描  述 :  ${TODO}
 */
public class TransferRecordActivity extends BaseActivity<TransferRecoderPresenter> implements TansferRecoderContact.View {

    @BindView(R.id.recyclerView)
    @NonNull
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    @NonNull
    SmartRefreshLayout mRefreshLayout;
    View errorView;

    View notDataView;

    TextView tvRefresh;

    @NonNull
    TransferRecordAdapter adapter;

    private boolean isRefresh;
    private int currentPage = 1;//当前页
    private int rows = 15;//每页显示条数

    @Override
    protected int getLayoutId() {
        return R.layout.activity_transfer_record;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new RecyclerListDivider(getResources().getColor(R.color.color_Line)));

        notDataView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) recyclerView.getParent(), false);
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) recyclerView.getParent(), false);
        tvRefresh = errorView.findViewById(R.id.tv_refresh);
        tvRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRefreshLayout.autoRefresh(0, 10, 1);
            }
        });

        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isRefresh = false;
                mPresenter.getTransferRecoder(++currentPage, rows);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isRefresh = true;
                currentPage = 1;
                mPresenter.getTransferRecoder(currentPage, rows);

            }
        });
    }

    @Override
    protected void initData() {
        initAdapter();
        mRefreshLayout.autoRefresh(0, 10, 1);
    }

    private void initAdapter() {
        if (adapter == null) {
            adapter = new TransferRecordAdapter();
        }
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected TransferRecoderPresenter createPresenter() {
        return new TransferRecoderPresenter(this);
    }

    @Override
    public void onSetAdapter(List<TransferRecoderBean.TransferRecoderData> list) {
        if (isRefresh) {
            mRefreshLayout.setEnableLoadMore(true);
            if (list != null && list.size() <= 0) {
                adapter.setEmptyView(notDataView);
            }
            adapter.setNewData(list);
        } else {
            if (list == null || list.size() < rows) {
                mRefreshLayout.setEnableLoadMore(false);
            } else {
                mRefreshLayout.setEnableLoadMore(true);
            }
            adapter.addData(list);
        }
    }

    @Override
    public void onShowNoMore() {
        if (mRefreshLayout != null) {
            mRefreshLayout.finishRefresh();
            mRefreshLayout.finishLoadMore();
        }
    }

    @Override
    public void onError(int code) {
        super.onError(code);
        if (isRefresh && adapter != null)
            adapter.setEmptyView(errorView);
    }
}
